﻿using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Mvc;
using System;

namespace IVBlog.Controllers
{
    public class FileController : Controller
    {
        //显示一个测试用的图片
        public ActionResult Index()
        {
            return View();
        }

        //显示图片
        //public ActionResult ViewPic(string id)
        //{
        //    if (string.IsNullOrEmpty(id) == true)
        //    {
        //        id = "nodata.png";
        //    }

        //    string rootPath = Server.MapPath("/App_Data/Pic/");
        //    string imgPath = rootPath + id;
        //    byte[] img = System.IO.File.ReadAllBytes(imgPath);
        //    return File(img, "application/octet-stream");
        //}

        //上传图片
        //public ActionResult UploadPic()
        //{
        //    PicReturn pr = new PicReturn();
        //    //pr.location = "/file/viewpic?id=nodata.png";
        //    try
        //    {
        //        if (Request.Files.Count > 0)
        //        {
        //            HttpPostedFileBase fitem = Request.Files[0];
        //            string ExtenName = System.IO.Path.GetExtension(fitem.FileName);
        //            string titleNEW = Guid.NewGuid().ToString() + ExtenName;
        //            string savePath = "../App_Data/Pic/" + titleNEW;
        //            savePath = System.Web.HttpContext.Current.Server.MapPath(savePath);

        //            fitem.SaveAs(savePath);
        //            pr.location = "/file/viewpic?id=" + titleNEW;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine(ex.Message);
        //    }

        //    return Json(pr);
        //}

    }
}


//using System.IO;
//using Microsoft.AspNetCore.Hosting;
//using Microsoft.AspNetCore.Mvc;
//using Newtonsoft.Json.Linq;

//namespace Youlike.Mall.Web.Controllers 
//{ 
//    [Route("editor/upload")] 
//    public class EditorController : Controller 
//    {
//        [System.Obsolete]
//        IHostingEnvironment environment;

//        [System.Obsolete]
//        public EditorController(IHostingEnvironment hostingEnvironment) 
//        { 
//            this.environment = hostingEnvironment; 
//        } 
//        [HttpPost]
//        [System.Obsolete]
//        public string Post() 
//        { 
//            var files = Request.Form.Files; 
//            if (files.Count == 0) 
//            { 
//                return "未选择文件"; 
//            } 
//            var file = files[0]; 
//            var path = environment.WebRootPath; 
//            var directory = "upfile";//上传文件存放的服务器目录
//            var newpath = Path.Combine(path, directory);
//            if (!Directory.Exists(newpath)) Directory.CreateDirectory(newpath);//目录不存在创建
//            var filename = file.FileName;
//            var filepath = Path.Combine(path, directory, filename);
//            using (var fs = new FileStream(filepath, FileMode.Create, FileAccess.Write))
//            {                
//                file.CopyTo(fs);                
//                fs.Close();
//            }            
//            var url = $"/{directory}/{filename}";  

//            //这里引用了Newtonsoft.Json处理
//            var res = JObject.FromObject(new ResponseFile() { location = url });           
//            return res.ToString();        
//        }    
//    }    
//    public class ResponseFile    
//    {        
//        public string location { get; set; }    
//    }
//}